import Vue from 'vue'
import {
  Button,
  Tabbar,
  TabbarItem,
  Icon,
  Search,
  Swipe,
  SwipeItem,
  Lazyload,
  Tab,
  Tabs,
  Form,
  Field,
  Switch,
  Empty,
  Toast,
  NavBar,
  Stepper,
  ActionSheet,
  Uploader,
  ImagePreview ,
  Tag,
  GoodsAction,
  GoodsActionIcon,
  GoodsActionButton,
  Sku,
  AddressList,
  AddressEdit,
  SubmitBar,
  NumberKeyboard,
  List,
  Cell,
  PullRefresh,
  Overlay,
  Divider,
  CountDown,
  Image,
  Calendar,
  Picker,
  DatetimePicker,
  Popup,
  PasswordInput,
  Loading,
  Progress,
  NoticeBar ,
  Dialog,
  Skeleton,
  Checkbox, CheckboxGroup
} from 'vant'

Vue.use(CountDown);
Vue.use(Divider);
Vue.use(Overlay);
Vue.use(Button);
Vue.use(Image);
Vue.use(Tabbar);
Vue.use(TabbarItem);
Vue.use(Icon);
Vue.use(Search);
Vue.use(Swipe);
Vue.use(SwipeItem);
Vue.use(Tab);
Vue.use(Tabs);
Vue.use(Form);
Vue.use(Field);
Vue.use(Switch);
Vue.use(Empty);
Vue.use(Toast);
Vue.use(NavBar);
Vue.use(Stepper);
Vue.use(ActionSheet);
Vue.use(Uploader);
Vue.use(ImagePreview);
Vue.use(Tag);
Vue.use(GoodsAction);
Vue.use(GoodsActionButton);
Vue.use(GoodsActionIcon);
Vue.use(Sku);
Vue.use(AddressList);
Vue.use(AddressEdit);
Vue.use(SubmitBar);
Vue.use(NumberKeyboard);
Vue.use(List);
Vue.use(Cell);
Vue.use(PullRefresh);
Vue.use(Calendar);
Vue.use(Picker);
Vue.use(DatetimePicker);
Vue.use(Popup);
Vue.use(PasswordInput);
Vue.use(Loading);
Vue.use(Progress)
Vue.use(NoticeBar)
Vue.use(Dialog);
Vue.use(Skeleton)
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
// 注册时可以配置额外的选项,懒加载时需要开启
// <img v-lazy="image" />
// Vue.use(Lazyload, {
//   lazyComponent: true, //是否懒加载模块
//   loading: require('@/assets/images/devimg/loading.gif'),
//   error: require('@/assets/images/devimg/errorimg.jpg'),
// });
//vue指令
Vue.directive('movable',{
  bind(el){
    let agent=window.navigator.userAgent.toLowerCase(),start,move,end
    //判断当前是移动端还是PC，移动端监听touch，PC监听mouse
    if(["android","iphone","symbianos","windows phone","ipad","ipod"].some(i=>agent.indexOf(i)>0)){ start='touchstart';move='touchmove';end='touchend' }else{ start='mousedown';move='mousemove';end='mouseup' }
    //添加事件开始监听
    var clientwidth = document.body.clientWidth  
    var clientHeight = document.body.clientHeight    
    el.addEventListener(start,e=>{
      //读取事件开始时，触摸或鼠标按下的坐标，以及容器当前所在位置，并声明两个函数对象，供后面添加和取消监听用
      let cur=e.touches?{x:e.touches[0].clientX,y:e.touches[0].clientY}:{x:e.clientX,y:e.clientY},x=el.offsetLeft,y=el.offsetTop,prevent=function(){event.preventDefault()},moveFun=function(){
        if(event.touches){  
          var currentx = event.touches[0].clientX-cur.x+x
          var currenty = event.touches[0].clientY-cur.y+y
          if(currentx<0){//左边界
            currentx = 0
          }
          if(currentx>clientwidth-100){//右边界
            currentx = clientwidth-100
          }
          if(currenty<0){//上边界
            currenty = 0
          }
          if(currenty>clientHeight-100){//下边界
            currenty = clientHeight-100
          }
          el.style.left = currentx+'px'
          el.style.top = currenty+'px'
        }else{
          var currentx =event.clientX-cur.x+x
          var currenty = event.clientY-cur.y+y
          if(currentx<0){//左边界
            currentx = 0
          }
          if(currentx>clientwidth-100){//右边界
            currentx = clientwidth-100
          }
          if(currenty<0){//上边界
            currenty = 0
          }
          if(currenty>clientHeight-100){//下边界
            currenty = clientHeight-100
          }
          el.style.left=currentx+'px'
          el.style.top=currenty+'px'
        }
        //阻止移动端屏幕移动
        window.addEventListener('touchmove',prevent,{passive:false})
      }
      //监听move并修改位置
      el.addEventListener(move,moveFun)
      //声明一个移除处理函数对象
      let endFun=function(){
        el.removeEventListener(move,moveFun)
        el.removeEventListener(move,endFun)
        window.removeEventListener('touchmove',prevent)
      }
      //监听到事件结束时，移除start以外的监听
      el.addEventListener(end,endFun)
    })
  }
})